package com.ikeke.es;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;

import java.net.InetAddress;
import java.net.InetSocketAddress;

public class ElasticSearchClientTest {

    private TransportClient client = null;

    @Before
    public void init() throws Exception{
        Settings settings = Settings.builder().put("cluster.name","my-elasticsearch").build();

        //创建一个client对象
        client = new PreBuiltTransportClient(settings)
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9301))
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9302))
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9303));
    }

    @Test
    public void createIndex() throws Exception{
        //创建配置信息设定集群名称
        Settings settings = Settings.builder().put("cluster.name","my-elasticsearch").build();

        //创建一个client对象
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9301));
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9302));
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9303));
        //创建一个索引库
        client.admin().indices().prepareCreate("index_hello")
                //执行操作
                .get();
        //关闭client对象
        client.close();

    };


    @Test
    public void setMappings() throws Exception{
        //创建配置信息设定集群名称
        Settings settings = Settings.builder().put("cluster.name","my-elasticsearch").build();

        //创建一个client对象
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9301));
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9302));
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9303));

        XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()
            .startObject()
                .startObject("article")
                    .startObject("properties")
                        .startObject("id")
                            .field("type","long")
                            .field("store",true)
                        .endObject()
                        .startObject("title")
                                .field("type","text")
                                .field("store",true)
                                .field("analyzer","ik_smart")
                         .endObject()
                         .startObject("content")
                                .field("type","text")
                                .field("store",true)
                                .field("analyzer","ik_smart")
                         .endObject()
                .endObject()
                .endObject()
            .endObject();

        //使用客户端添加mapping到数据库


        //创建一个索引库
        client.admin().indices()
                //设置要做映射的索引
                .preparePutMapping("index_hello")
                //设置要做映射的type
                .setType("article")
                //mapping 信息，可以是XContentBuilder对象 也可以是json格式的字符串
                .setSource(xContentBuilder)
                //执行操作
                .get();
        //关闭client对象
        client.close();

    };

    //添加文档
    @Test
    public void testAddDocument() throws Exception{
        XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()
                .startObject()
                .field("id",2L)
                .field("title","新华社北京9月5日电 第四届中国－阿拉伯国家博览会9月5日在宁夏银川开幕")
                .field("content","习近平强调，去年7月，我在中阿合作论坛第八届部长级会议开幕式上宣布建立全面合作、共同发展")
                .endObject();
        //添加文档
        client.prepareIndex("index_hello","article","2").setSource(xContentBuilder).get();
        //关闭client对象
        client.close();

    };

    //添加文档2
    @Test
    public void testAddDocument2() throws Exception{

        Article article = new Article();
        article.setId(3L);
        article.setTitle("澳门特区第五任行政长官候任人贺");
        article.setContent("澳门特区公报9月2日刊登澳门特区终审法院有关澳门特别行政区第五任行政长官选举结果的公告");

        ObjectMapper objectMapper = new ObjectMapper();
        String  jsonDocum = objectMapper.writeValueAsString(article);

        //添加文档
        client.prepareIndex("index_hello","article","3").setSource(jsonDocum,XContentType.JSON).get();
        //关闭client对象
        client.close();

    };

    //添加文档2
    @Test
    public void testAddDocument3() throws Exception{
        for (int x=4 ;x < 100;x++){
            Article article = new Article();
            article.setId(x);
            article.setTitle("的哥曝嘀嗒预约单买卖内幕：一单最高可卖到100元"+x);
            article.setContent("遇到这种情况，或许意味着你的订单信息被人卖了" + x);

            ObjectMapper objectMapper = new ObjectMapper();
            String  jsonDocum = objectMapper.writeValueAsString(article);

            //添加文档
            client.prepareIndex("index_hello","article",x+"").setSource(jsonDocum,XContentType.JSON).get();
        }

        //关闭client对象
        client.close();

    };

}
